package test;


import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.lock.R;

public class AppInfoView extends View{
	
	private StateListDrawable mDrawableImage;
    private String mText;
    private int mTextSize;
    private int mTextColor;
    private Paint mProgressPaint;
	
	
	
	public AppInfoView(Context context) {
        this(context, null);
    }

    public AppInfoView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public AppInfoView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, -1);
    }

    public AppInfoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        TypedArray a = null;
        try {
            a = getContext().obtainStyledAttributes(attrs, R.styleable.AppInfoView);
            mDrawableImage = (StateListDrawable)a.getDrawable(R.styleable.AppInfoView_drawable_iamge);
            mText = a.getString(R.styleable.AppInfoView_text);
            mTextSize = a.getDimensionPixelSize(R.styleable.AppInfoView_textSize, 20);
            mTextColor = a.getColor(R.styleable.AppInfoView_textColor, Color.WHITE);
            initPaint();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            a.recycle();
        }
    }

    /**
     * onResume()之后执行
     */
    @Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		setClickable(true);
	}

	private void initPaint() {
        mProgressPaint = new Paint();
        mProgressPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setColor(Color.WHITE);
        mProgressPaint.setTextSize(mTextSize);
        mProgressPaint.setTextAlign(Paint.Align.CENTER);
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        updateDrawable(w, h);
    }

    private void updateDrawable(int w, int h) {
        if (mDrawableImage != null) {
            int dWidth = mDrawableImage.getIntrinsicWidth();
            int dHeight = mDrawableImage.getIntrinsicHeight();
            int leftX = (getWidth()-dWidth)/2;//图片左上角X坐标
            int leftY = 0;//图片左上角Y坐标
            int rightX = dWidth+leftX;//图片右下角X坐标
            int rightY = dHeight+leftY;//图片右下角Y坐标
            mDrawableImage.setBounds(leftX,leftY,rightX,rightY);
            mDrawableImage.setCallback(this);
        }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mDrawableImage != null) {
            mDrawableImage.draw(canvas);
        }



        mProgressPaint.setColor(mTextColor);
        Paint.FontMetrics fontMetrics = mProgressPaint.getFontMetrics();
        float tY = mDrawableImage.getIntrinsicHeight()+10+fontMetrics.bottom-fontMetrics.top;
        Log.d("wxy", "image h="+mDrawableImage.getIntrinsicHeight()+",text h="+(fontMetrics.bottom-fontMetrics.top));
        canvas.drawText(mText, getWidth() / 2, tY, mProgressPaint);
    }


    /**
     * view的视图状态发生改变时调用，例如：press
     */
    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        mDrawableImage.setState(getDrawableState());
        invalidate();
    }


}
